package com.jiezuo.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.auth.entity.UserEntry;
import com.jiezuo.auth.entity.UserEntrySet;
import com.jiezuo.auth.mapper.UserEntryMapper;
import com.jiezuo.auth.service.UserEntryService;
import com.jiezuo.auth.service.UserEntrySetService;
import com.jiezuo.common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 用户自定义列表
 * @date 2023/11/22 16:39
 **/
@Transactional
@Slf4j
@Service
public class UserEntryServiceImpl extends ServiceImpl<UserEntryMapper, UserEntry> implements UserEntryService {
    @Autowired
    private UserEntrySetService setService;

    @Override
    public IPage<UserEntry> findPage(Page initMpPage, UserEntry userEntry) {
        QueryWrapper<UserEntry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        if (StringUtil.isNotEmpty(userEntry.getUser_id())) {
            queryWrapper.eq("user_id", userEntry.getUser_id());
        }
        if (StringUtil.isNotEmpty(userEntry.getEntry_unique())) {
            queryWrapper.eq("entry_unique", userEntry.getEntry_unique());
        }
        IPage<UserEntry> page = this.page(initMpPage, queryWrapper);
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            List<String> entryIdList = page.getRecords().stream().map(item -> item.getId()).collect(Collectors.toList());
            QueryWrapper<UserEntrySet> setQuery = new QueryWrapper<>();
            setQuery.in("entry_id", entryIdList);
            setQuery.eq("is_delete", "0");
            List<UserEntrySet> setList = setService.list(setQuery.orderByAsc("sort"));
            setList.forEach(set -> {
                if (StringUtil.isNotEmpty(set.getIs_visible()) && "1".equals(set.getIs_visible())) {
                    set.setVisible(Boolean.TRUE);
                } else {
                    set.setVisible(Boolean.FALSE);
                }

                if (StringUtil.isNotEmpty(set.getIs_showOverflow()) && "1".equals(set.getIs_showOverflow())) {
                    set.setShowOverflow(Boolean.TRUE);
                } else {
                    set.setShowOverflow(Boolean.FALSE);
                }

                if (StringUtil.isNotEmpty(set.getIs_sortable()) && "1".equals(set.getIs_sortable())) {
                    set.setSortable(Boolean.TRUE);
                } else {
                    set.setSortable(Boolean.FALSE);
                }
            });
            page.getRecords().forEach(entry -> {
                entry.setSetList(setList.stream().filter(set -> entry.getId().equals(set.getEntry_id())).collect(Collectors.toList()));
            });
        }
        return page;
    }

    @Override
    public List<UserEntry> findList(UserEntry userEntry) {
        QueryWrapper<UserEntry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        if (StringUtil.isNotEmpty(userEntry.getUser_id())) {
            queryWrapper.eq("user_id", userEntry.getUser_id());
        }
        if (StringUtil.isNotEmpty(userEntry.getEntry_unique())) {
            queryWrapper.eq("entry_unique", userEntry.getEntry_unique());
        }
        List<UserEntry> list = this.list(queryWrapper);
        if (list != null && list.size() > 0) {
            List<String> entryIdList = list.stream().map(item -> item.getId()).collect(Collectors.toList());
            QueryWrapper<UserEntrySet> setQuery = new QueryWrapper<>();
            setQuery.in("entry_id", entryIdList);
            setQuery.eq("is_delete", "0");
            List<UserEntrySet> setList = setService.list(setQuery.orderByAsc("sort"));
            setList.forEach(set -> {
                if (StringUtil.isNotEmpty(set.getIs_visible()) && "1".equals(set.getIs_visible())) {
                    set.setVisible(Boolean.TRUE);
                } else {
                    set.setVisible(Boolean.FALSE);
                }

                if (StringUtil.isNotEmpty(set.getIs_showOverflow()) && "1".equals(set.getIs_showOverflow())) {
                    set.setShowOverflow(Boolean.TRUE);
                } else {
                    set.setShowOverflow(Boolean.FALSE);
                }

                if (StringUtil.isNotEmpty(set.getIs_sortable()) && "1".equals(set.getIs_sortable())) {
                    set.setSortable(Boolean.TRUE);
                } else {
                    set.setSortable(Boolean.FALSE);
                }
            });
            list.forEach(entry -> {
                entry.setSetList(setList.stream().filter(set -> entry.getId().equals(set.getEntry_id())).collect(Collectors.toList()));
            });
        }
        return list;
    }

    @Override
    public UserEntry findById(String id) {
        UserEntry entry = this.getById(id);
        QueryWrapper<UserEntrySet> setQuery = new QueryWrapper<>();
        setQuery.in("entry_id", id);
        setQuery.eq("is_delete", "0");
        List<UserEntrySet> setList = setService.list(setQuery);
        setList.forEach(set -> {
            if (StringUtil.isNotEmpty(set.getIs_visible()) && "1".equals(set.getIs_visible())) {
                set.setVisible(Boolean.TRUE);
            } else {
                set.setVisible(Boolean.FALSE);
            }

            if (StringUtil.isNotEmpty(set.getIs_showOverflow()) && "1".equals(set.getIs_showOverflow())) {
                set.setShowOverflow(Boolean.TRUE);
            } else {
                set.setShowOverflow(Boolean.FALSE);
            }

            if (StringUtil.isNotEmpty(set.getIs_sortable()) && "1".equals(set.getIs_sortable())) {
                set.setSortable(Boolean.TRUE);
            } else {
                set.setSortable(Boolean.FALSE);
            }
        });
        entry.setSetList(setList);
        return entry;
    }

    @Override
    public UserEntry add(UserEntry userEntry) {
        this.saveOrUpdate(userEntry);
        for (int i = 1; i <= userEntry.getSetList().size(); i++) {
            UserEntrySet set = userEntry.getSetList().get(i - 1);
            set.setEntry_id(userEntry.getId());
            set.setSort(i + "");
            if (Boolean.FALSE.equals(set.getVisible())) {
                set.setIs_visible("0");
            } else {
                set.setIs_visible("1");
            }
            if (Boolean.FALSE.equals(set.getShowOverflow())) {
                set.setIs_showOverflow("0");
            } else {
                set.setIs_showOverflow("1");
            }
            if (Boolean.FALSE.equals(set.getSortable())) {
                set.setIs_sortable("0");
            } else {
                set.setIs_sortable("1");
            }
        }

        if (userEntry.getSetList() != null && userEntry.getSetList().size() > 0) {
            setService.saveOrUpdateBatch(userEntry.getSetList());
        }
        userEntry.setSetList(null);
        QueryWrapper<UserEntrySet> setQuery = new QueryWrapper<>();
        setQuery.in("entry_id", userEntry.getId());
        setQuery.eq("is_delete", "0");
        List<UserEntrySet> setList = setService.list(setQuery.orderByAsc("sort"));
        setList.forEach(set -> {
            if (StringUtil.isNotEmpty(set.getIs_visible()) && "1".equals(set.getIs_visible())) {
                set.setVisible(Boolean.TRUE);
            } else {
                set.setVisible(Boolean.FALSE);
            }

            if (StringUtil.isNotEmpty(set.getIs_showOverflow()) && "1".equals(set.getIs_showOverflow())) {
                set.setShowOverflow(Boolean.TRUE);
            } else {
                set.setShowOverflow(Boolean.FALSE);
            }

            if (StringUtil.isNotEmpty(set.getIs_sortable()) && "1".equals(set.getIs_sortable())) {
                set.setSortable(Boolean.TRUE);
            } else {
                set.setSortable(Boolean.FALSE);
            }
        });
        userEntry.setSetList(setList);
        return userEntry;
    }

    @Override
    public boolean update(UserEntry userEntry) {
        return this.updateById(userEntry);
    }

    @Override
    public boolean delete(String id) {
        UserEntry userEntry = new UserEntry();
        userEntry.setId(id);
        userEntry.setIs_delete("1");
        return this.updateById(userEntry);
    }

    public static void main(String[] args) {
        Boolean is = null;
        if (Boolean.FALSE.equals(is)) {
            System.out.println("args = [" + false + "]");
        } else {
            System.out.println("args = [" + true + "]");
        }
    }

}
